package com.example.wanjialei.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.example.wanjialei.mapper.StockMapper;
import com.example.wanjialei.pojo.Stock;
import com.example.wanjialei.service.StockService;
import com.example.wanjialei.service.serviceimpl.StockImpl;
import org.springframework.boot.autoconfigure.mongo.embedded.DownloadConfigBuilderCustomizer;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@CrossOrigin
@RestController
public class StockController {
    @Resource
    private StockService stockService ;
    @Resource
    private StockMapper stockMapper;

    @RequestMapping("/getAllStockInfo")
    public Map<String, Object> getStockInfo(  Stock stock){
        return stockService.getStockInfo(stock);
    }
    @RequestMapping("/approvedBysno")
    public Map<String,Object> approvedBysno(@RequestBody Stock stock){
        return stockService.approvedBysno(stock);
    }
    @RequestMapping("/rejectedBysno")
    public Map<String,Object> rejectedBysno(@RequestBody Stock stock){
        return stockService.rejectedBysno(stock);
    }
    @RequestMapping("/recheckBysno")
    public Map<String,Object> recheckBysno(@RequestBody Stock stock){
        return stockService.recheckBysno(stock);
    }
    @RequestMapping("/huizongBysno")
    public Map<String,Object> huizongBysno(@RequestBody Stock stock){
        return stockService.huizongBysno(stock);
    }
    @RequestMapping("/export")
    public void weiteExcel(HttpServletResponse response, HttpServletRequest request) throws IOException{
        request.getParameterValues("sno");
        List stocks = new ArrayList();
        stocks = stockService.getStockInfo2();
        
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("备货计划订单列表","UTF-8");
        ServletOutputStream outputStream = response.getOutputStream();
//        获取当前时间
        DateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date();
        String nowTime = dateTimeFormatter.format(date);
//        导出名字
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+nowTime+".xlsx");
        ExcelWriterBuilder writerBuilder = EasyExcel.write(outputStream,Stock.class);
        ExcelWriterSheetBuilder sheet = writerBuilder.sheet();
        sheet.doWrite(stocks);
    }
}
